package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.MomentsApi;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.server.pojo.User;
import com.tanhua.server.utils.UserThreadLocal;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName :  MomentMQService
 * @Author : Tenebrous
 * @Date: 2020/12/18 16:42
 * @Description : rocketMQ业务层
 */
@Service
public class MomentMqService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MomentMqService.class);

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Reference(version = "1.0.0")
    private MomentsApi momentsApi;

    /**
     * 新增动态消息
     * @param publishId         动态id
     */
    public void sendSavePublishMessage(String publishId) {
        this.sendMessage(1, publishId);
    }

    /**
     * 浏览动态消息
     * @param publishId         动态id
     */
    public void sendQueryPublishMessage(String publishId) {
        this.sendMessage(2, publishId);
    }

    /**
     * 点赞动态消息
     * @param publishId         动态id
     */
    public void sendLikePublishMessage(String publishId) {
        this.sendMessage(3, publishId);
    }

    /**
     * 点赞动态消息
     * @param publishId         动态id
     */
    public void sendLovePublishMessage(String publishId) {
        this.sendMessage(4, publishId);
    }

    /**
     * 评论动态消息
     * @param publishId         动态id
     */
    public void sendCommentPublishMessage(String publishId) {
        this.sendMessage(5, publishId);
    }

    /**
     * 取消点赞动态消息
     * @param publishId         动态id
     */
    public void sendDisLikePublishMessage(String publishId) {
        this.sendMessage(6, publishId);
    }

    /**
     * 取消喜欢动态消息
     * @param publishId         动态id
     */
    public void sendDisLovePublishMessage(String publishId) {
        this.sendMessage(7, publishId);
    }

    /**
     * 发送消息
     * @param type          1-发动态  2-浏览动态  3-点赞  4-喜欢  5-评论  6-取消点赞  7-取消喜欢
     * @param publishId     动态id
     */
    private void sendMessage(Integer type, String publishId) {
        try {
            // 获取用户信息
            User user = UserThreadLocal.get();
            // 获取publish表，然后获取pid
            Publish publish = this.momentsApi.queryPublishById(publishId);
            // 消息体
            Map<String, Object> msg = new HashMap<>();
            msg.put("type", type);
            msg.put("publishId", publishId);
            msg.put("date", System.currentTimeMillis());
            msg.put("userId", user.getId());
            msg.put("pid", publish.getPid());
            // 发送消息
            this.rocketMQTemplate.convertAndSend("tanhua-quanzi", msg);
        } catch (Exception e) {
            LOGGER.error("圈子消息发送时效！ type = " + type + ", publishId = " + publishId, e);
        }
    }

}
